<?php namespace Summer\Hub;

use System\Classes\PluginBase;
use Validator;

class Plugin extends PluginBase
{
    public function registerComponents()
    {
    }

    public function registerSettings()
    {
    }

    public function boot()
    {
        //注册自动验证
        Validator::extend('mobile', function($attribute, $value, $parameters) {
            return preg_match('/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|(19[0-9])|(147))\d{8}$/', $value);
        });
        //自定义报错信息
        Validator::replacer('mobile', function ($message, $attribute, $rule, $parameters) {
            return '手机号格式不对';
        });
        //正数验证规则
        Validator::extend('positive', function($attribute, $value, $parameters) {
            return $value>0?true:false;
        });
    }
    /**
     * Register custom list type
     *
     * @return array
     */
    public function registerListColumnTypes()
    {
        return [
            'hub_list_img' => [ListImgField::class, 'render'],
            'hub_list_media_img' => [ListMediaImgField::class, 'render'],
            'hub_list_imgs' => [ListImgsField::class, 'render'],
            'hub_list_voice' => [ListVoiceField::class, 'render'],
            'hub_list_video' => [ListVideoField::class, 'render'],
            'hub_list_button' => [ListButtonField::class, 'render'],
        ];
    }
    // FORM WIDGETS
    public function registerFormWidgets(){
        return [
            'Summer\Hub\FormWidgets\Showimg' => [
                'label' => '显示图片',
                'code'  => 'hub_form_showimg'
            ],
            'Summer\Hub\FormWidgets\Powerange' => [
                'label' => '滑块',
                'code'  => 'hub_form_powerange'
            ],
        ];
    }

    /**
     * 首页小部件
     * @return array|\string[][]
     */
    public function registerReportWidgets()
    {
        return [
            'Summer\Hub\ReportWidgets\DataInit' => [
                'label'       => '数据初始化',
                'context'     => 'dashboard',
            ],
        ];
    }
}
